package com.app.controller;

import com.app.enums.StatusCodeEnum;
import com.app.model.ApplyLoanModel;
import com.app.model.JiuquanUserModel;
import com.app.model.LoanInfoModel;
import com.app.service.ApplyLoanService;
import com.app.service.LoanInfoService;
import com.app.service.UserService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.ApplyLoanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

//申请贷款
@RestController
@Controller
@RequestMapping("/jiuquan/applyLoan")
public class ApplyLoanController {
    @Autowired
    private ApplyLoanService applyLoanService;

    @Autowired
    private LoanInfoService loanInfoService;

    @Autowired
    private UserService userService;

    //查询列表
    @GetMapping("/getApplyLoanList")
    public Result getApplyLoanList(@RequestParam(defaultValue = "0") Integer page,
                                   @RequestParam(defaultValue = "10") Integer limit,String startTime,String endTime,String name,Integer loanId,Integer areaId,String identity) {
        ApplyLoanVo applyLoanList = applyLoanService.getApplyLoanList(page, limit,startTime,endTime,name,loanId,areaId,identity);
        return Result.success(applyLoanList);
    }

    //详情
    @GetMapping("/getApplyLoanById")
    public Result getApplyLoanById(Integer id) {
        ApplyLoanModel applyLoan = applyLoanService.getById(id);
        Integer loanId = applyLoan.getLoanId();
        LoanInfoModel loanInfo = loanInfoService.getById(loanId);
        applyLoan.setLoanIdText(loanInfo.getTitle());
        return Result.success(applyLoan);
    }

    //新增和修改
    @PostMapping("/addAndUpdateApplyLoan")
    public Result addAndUpdateApplyLoan(@RequestBody ApplyLoanModel applyLoanModel) {
        ApplyLoanModel applyLoan = applyLoanService.getById(applyLoanModel.getId());
        if (applyLoan != null) {
            applyLoanModel.setUpdateTime(CommonHelp.getNowTimestamp());
            applyLoanService.updateById(applyLoanModel);
            return Result.success("修改成功");
        } else {
            applyLoanModel.setCreateTime(CommonHelp.getNowTimestamp());
            applyLoanService.save(applyLoanModel);
            return Result.success("添加成功");
        }
    }

    //用户查询列表(小程序)
    @GetMapping("/getUserApplyLoanList")
    public Result getUserApplyLoanList(@RequestParam(defaultValue = "0") Integer page,
                                       @RequestParam(defaultValue = "10") Integer limit,HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(StatusCodeEnum.STATUS405.getCode(), StatusCodeEnum.STATUS405.getMsg());
        }
        String openId = TokenUtils.verify(token);
        JiuquanUserModel user = userService.getUserByOpenid(openId);
        if (user == null) {
            return Result.error(StatusCodeEnum.STATUS406.getCode(), StatusCodeEnum.STATUS406.getMsg());
        }

        ApplyLoanVo applyLoanList = applyLoanService.getUserApplyLoanList(page, limit,openId);
        return Result.success(applyLoanList);
    }
    //用户提交贷款申请
    @PostMapping("/userAddApplyLoan")
    public Result userAddApplyLoan(@RequestBody ApplyLoanModel applyLoanModel, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(StatusCodeEnum.STATUS405.getCode(), StatusCodeEnum.STATUS405.getMsg());
        }
        String openId = TokenUtils.verify(token);
        JiuquanUserModel user = userService.getUserByOpenid(openId);
        if (user == null) {
            return Result.error(StatusCodeEnum.STATUS406.getCode(), StatusCodeEnum.STATUS406.getMsg());
        }
        applyLoanModel.setCreateTime(CommonHelp.getNowTimestamp());
        applyLoanModel.setOpenId(openId);
        applyLoanService.save(applyLoanModel);
        return Result.success("添加成功");
    }

    //删除
    @GetMapping("/delApplyLoan")
    public Result delApplyLoan(Integer id) {
        applyLoanService.removeById(id);
        return Result.success("删除成功");
    }
}
